package springapp.web.controllers;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import springapp.web.data.Tag;
import springapp.web.data.VideoFile;
import springapp.web.exceptions.ValidationException;
import springapp.web.managers.TagManager;
import springapp.web.managers.VideoManager;
import springapp.web.utils.MV;
import springapp.web.utils.Utils;

/**
 * @author sad
 */
@Controller
public class MainPageController extends BaseController {

    @Autowired
    private VideoManager videoManager;
    @Autowired
    private TagManager tagManager;

    @RequestMapping(value = "index.html", method = RequestMethod.GET)
    public ModelAndView mainPage() {
        List<VideoFile> videos = videoManager.getVideos();
        return new MV("index.jsp", "videos", videos);
    }

    @RequestMapping(value = "showVideo/{videoId}.html", method = RequestMethod.GET)
    public ModelAndView showVideoPage(@PathVariable Long videoId) {
        VideoFile video = videoManager.findById(videoId);
        if (video == null) {
            throw new ValidationException("This video is not found");
        }

        return new MV("showVideo.jsp", "video", video);
    }

    @RequestMapping(value = "createTag.html", method = RequestMethod.POST)
    public @ResponseBody
    String createTagAjaxHandler(@RequestBody String value) throws UnsupportedEncodingException {
        String request = java.net.URLDecoder.decode(value, "UTF-8");
        JsonParser parser = new JsonParser();
        request = request.substring(0, request.length() - 1);
        JsonObject element = parser.parse(request).getAsJsonObject();
        String comment = element.get("comment").getAsString();
        String fileUri = element.get("fileUri").getAsString();
        String tags = element.get("tags").getAsString();
        Tag tag = new Tag();
        tag.setComment(comment);
        tag.setFileName(fileUri);
        tag.setTags(tags);
        tag.setCode(Utils.csRandomAlphaNumericString(6));
        tagManager.saveOrUpdate(tag);
        String url = "http://172.26.52.26/app/v/" + tag.getCode() + ".html";
        return String.format("{\"url\":\"%s\"}", url);
    }

    @RequestMapping(value = "v/{key}.html", method = RequestMethod.GET)
    public ModelAndView showVideoByTagLink(@PathVariable String key) throws UnsupportedEncodingException {
        Tag tag = tagManager.getTagByCode(key);
        if (tag == null) {
            throw new ResourceNotFoundException();
        }

        return new MV("showVideoByTag.jsp", "tag", tag);
    }
}